from models import db
from sqlalchemy import Column, Integer, String

# 模型之间的关系不需要创建第三个模型类来实现第三张关系表创建
# 创建用户和角色的关系表
user_role = db.Table('user_role',
                     Column('user_id', Integer, db.ForeignKey('user.id')),
                     Column('role_id', Integer, db.ForeignKey('role.id')))


class BaseModel(db.Model):
    __abstract__ = True  # 作用:不会创建模型的对应的表
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(50), unique=True, nullable=False)


class Role(BaseModel):
    __tablename__ = 'role'


class QX(BaseModel):
    pass


class User(db.Model):
    id = Column(Integer, primary_key=True, autoincrement=True)
    phone = Column(String(50), unique=True, nullable=False)
    auth_key = Column(String(100), nullable=False)
    nick_name = Column(String(50), nullable=False)
    photo = Column(String(100))
    #多对多的关系,指定secondary设置关联的表,Table()
    roles = db.relationship(Role, secondary=user_role)
